using UnityEngine;
using System.Collections;

public class UtilsRect{

	public static bool contains(Rect r1, Rect r2){
		float w = r1.width;
		float h = r1.height;
		float W = r2.width;
		float H = r2.height;
		if (w==0 || W==0 || h==0 || H==0) return false;
		
		// Note: if any dimension is zero, tests below must return false...
		float x = r1.x;
		float y = r1.y;
		float X = r2.x;
		float Y = r2.y;
		if (X < x || Y < y) return false;
		
		w += x;
		W += X;
		if (W <= X) {
		    if (w >= x || W > w) return false;
		} else {
		    if (w >= x && W > w) return false;
		}
		
		h += y;
		H += Y;
		if (H <= Y) {
		    if (h >= y || H > h) return false;
		} else {
		    if (h >= y && H > h) return false;
		}
		return true;
	}
	public static bool intersects(Rect r1, Rect r2){
		if (r1.xMin	 >= r2.xMax || r1.xMax <= r2.xMin || r1.yMin >= r2.yMax || r1.yMax <= r2.yMin) return false;
		else return true;
	}
	
	public static Rect addRects(Rect r1, Rect r2){
		return Rect.MinMaxRect(	r1.xMin<r2.xMin ? r1.xMin : r2.xMin,	
			                    r1.yMin<r2.yMin ? r1.yMin : r2.yMin,
			                    r1.xMax>r2.xMax ? r1.xMax : r2.xMax,
			                    r1.yMax>r2.yMax ? r1.yMax : r2.yMax );
	}
	
	public static Rect inflateRect(Rect r, float amount){
		return Rect.MinMaxRect(r.xMin*amount,r.yMin*amount,r.xMax*amount,r.yMax*amount);
	}
	public static Rect inflateRect(Rect r, float amount, Vector2 origin){
		r.x -= origin.x;
		r.y -= origin.y;
		Rect r1 = Rect.MinMaxRect(r.xMin*amount,r.yMin*amount,r.xMax*amount,r.yMax*amount);
		r1.x += origin.x;
		r1.y += origin.y;
		
		return r1;
	}	
}

